window.Utils = {
    // cc.Class({
    // name: 'Utils',

    javaToJs(id) {
        const ANDROIDApi = require('ANDROIDApi');
        ANDROIDApi.payOk(id)
    },

    javaToJsPro(accessToken,openid) {

        cc.sys.localStorage.setItem('OpenId', openid);
        let data = {
            accessToken: accessToken,
            openId: openid
        }
        EventManager.emit("GetUserInfo", JSON.stringify(data));
    },

    javaToJsFail(id){
        const ANDROIDApi = require('ANDROIDApi');
        ANDROIDApi.payFail(id)
    },

    stringFormat(args) {
        let out = arguments[0];
        if (arguments.length >= 1) {
            for (let key in arguments) {
                if (arguments[key] !== undefined && key > 0) {
                    out = out.replace('{' + (key - 1) + '}', arguments[key]);
                }
            }
        }
        return out;
    },

    versionStrToInt(ver) {
        let verArr = ver.split('.');
        return verArr.reduce((total, cur, index) => {
            return (total + Number(cur) * Math.pow(10, verArr.length - index - 1));
        }, 0);
    },
    //获取当前时间，格式YYYY-MM-DD
    getNowFormatDate() {
        let date = new Date();
        // let seperator1 = "-";
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        // let currentdate = year + seperator1 + month + seperator1 + strDate;
        let currentdate = year + month + strDate;
        return currentdate;
    },

    isSecondDay() {
        // 计算是否到了第二天
        let yesterday = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('yesterday') : cc.sys.localStorage.getItem('yesterday');
        yesterday = yesterday ? yesterday : 0;
        let today = Utils.getNowFormatDate();
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('yesterday', today);
        } else {
            cc.sys.localStorage.setItem('yesterday', today);
        }

        if (Number(today) > Number(yesterday)) {
            // this.clearTodaySigned();
            // this.clearTodayLuckdrawNum();
            return true;
        } else {
            return false;
        }


    },

    hadRedPacket() {
        let num = cc.sys.localStorage.getItem('hadRedPacket');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            num = wx.getStorageSync('hadRedPacket');
        }
        if (num) return true;
        return false;
    },

    setRedPacket() {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('hadRedPacket', 'true')
        } else {
            cc.sys.localStorage.setItem('hadRedPacket', 'true');
        }
    },

    clearRedPacket() {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.removeStorageSync('hadRedPacket')
        } else {
            cc.sys.localStorage.removeItem('hadRedPacket');
        }
    },

    getRedPacketNum() {
        let num = cc.sys.localStorage.getItem('RedPacketNum');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            num = wx.getStorageSync('RedPacketNum');
        }
        num = (num && num != "NaN") ? Number(num) : 0;
        return num;
    },

    addRedPacketNum(value) {
        let num = cc.sys.localStorage.getItem('RedPacketNum');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            num = wx.getStorageSync('RedPacketNum');
        }
        num = (num && num != "NaN") ? Number(num) : 0;
        num += value;
        // num += '.00';
        cc.sys.localStorage.setItem('RedPacketNum', num);
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('RedPacketNum', num);
        }
    },

    reduceRedPacketNum(value) {
        let num = cc.sys.localStorage.getItem('RedPacketNum');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            num = wx.getStorageSync('RedPacketNum');
        }
        num = (num && num != "NaN") ? Number(num) : 0;
        num -= value;
        // num += '.00';
        if (num <= 0) num = 0;
        cc.sys.localStorage.setItem('RedPacketNum', num);
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('RedPacketNum', num);
        }
    },

    getHadExchangeRedPacketNum() {
        let num = cc.sys.localStorage.getItem('HadExchangeRedPacketNum');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            num = wx.getStorageSync('HadExchangeRedPacketNum');
        }
        num = (num && num != "NaN") ? Number(num) : 0;
        let value = this.randomNum(5, 9);
        num += value;
        if (num >= 50) num = 50;
        cc.sys.localStorage.setItem('HadExchangeRedPacketNum', num);
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('HadExchangeRedPacketNum', num);
        }
        return num;
    },

    clearHadExchangeRedPacketNum() {
        cc.sys.localStorage.setItem('HadExchangeRedPacketNum', 0);
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('HadExchangeRedPacketNum', 0);
        }
    },

    getPlayVideoNum() {
        let num = cc.sys.localStorage.getItem('PlayVideoNum');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            num = wx.getStorageSync('PlayVideoNum');
        }
        num = (num && num != "NaN") ? Number(num) : 0;
        return Number(num);
    },

    addPlayVideoNum() {
        let num = cc.sys.localStorage.getItem('PlayVideoNum');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            num = wx.getStorageSync('PlayVideoNum');
        }
        num = (num && num != "NaN") ? Number(num) : 0;
        num++;
        cc.sys.localStorage.setItem('PlayVideoNum', num);
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('PlayVideoNum', num);
        }
    },

    clearPlayVideoNum() {
        cc.sys.localStorage.setItem('PlayVideoNum', 0);
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('PlayVideoNum', 0);
        }
    },

    //生成从minNum到maxNum的随机数
    randomNum(minNum, maxNum) {
        return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
    },

    // 登录MD5码
    getLoginMD5Code() {
        let loginMD5Code = cc.sys.localStorage.getItem('LOGIN_MD5CODE')
        if (cc.sys.platform === cc.sys.WECHAT_GAME) loginMD5Code = wx.getStorageSync('LOGIN_MD5CODE');
        if (loginMD5Code == null || loginMD5Code === "" || loginMD5Code == undefined) return '';
        return loginMD5Code;
    },

    // 获取当前时区
    clientTimeZone() {
        let munites = new Date().getTimezoneOffset();
        let hour = parseInt(munites / 60);
        let munite = munites % 60;
        let prefix = "-";
        if (hour < 0 || munite < 0) {
            prefix = "+";
            hour = -hour;
            if (munite < 0) {
                munite = -munite;
            }
        }
        hour = hour + "";
        munite = munite + "";
        if (hour.length == 1) {
            hour = "0" + hour;
        }
        if (munite.length == 1) {
            munite = "0" + munite;
        }
        return prefix + hour + munite;
    },
    // 获取当前时间 距1970 年 1 月 1 日之间的毫秒数
    getTime() {
        let myDate = new Date();
        return myDate.getTime();
    },
    // 获取屏幕分辨率
    getScreenResolution() {
        let Resolution = { width: cc.winSize.width, height: cc.winSize.height };
        if (cc.sys.isBrowser) {
            Resolution = { width: window.screen.width, height: window.screen.height }
        }
        return Resolution;
    },

    // 数组从小到大排序
    arrSort(arr) {
        for (let i = 0; i < arr.length - 1; i++) {
            for (let j = i + 1; j < arr.length; j++) {
                let cur = arr[i];
                if (cur > arr[j]) {
                    let index = arr[j];
                    arr[j] = cur;
                    arr[i] = index;
                }
            }
        }
        return arr;
    },
    // 数组重新排序并去重
    deDuplication(arr) {
        let mArr = [];
        for (let i = 0; i < arr.length; i++) {
            if (mArr.indexOf(arr[i]) == -1) {
                mArr.push(arr[i]);
            }
        }
        mArr = this.arrSort(mArr);
        return mArr;
    },

    // 只能使用一次
    isFirstLogin() {
        let isFirstLogin = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('isFirstLogin') : cc.sys.localStorage.getItem('isFirstLogin');
        if (isFirstLogin) return false;
        cc.sys.localStorage.setItem('isFirstLogin', 'true');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('isFirstLogin', 'true')
        }
        return true;
    },

    // isFirstPopRedPackage() {
    //     let result = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('isFirstPopRedPackage') : cc.sys.localStorage.getItem('isFirstPopRedPackage');
    //     if (result) return false;
    //     cc.sys.localStorage.setItem('isFirstPopRedPackage', 'true');
    //     if (cc.sys.platform === cc.sys.WECHAT_GAME) {
    //         wx.setStorageSync('isFirstPopRedPackage', 'true')
    //     }
    //     return true;
    // },

    // isFirstOpenRedPackage() {
    //     let result = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('isFirstOpenRedPackage') : cc.sys.localStorage.getItem('isFirstOpenRedPackage');
    //     if (result) return false;
    //     cc.sys.localStorage.setItem('isFirstOpenRedPackage', 'true');
    //     if (cc.sys.platform === cc.sys.WECHAT_GAME) {
    //         wx.setStorageSync('isFirstOpenRedPackage', 'true')
    //     }
    //     return true;
    // },


    // getRedActiveOpenTimes() {
    //     let num = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('RedActiveOpenTimes') : cc.sys.localStorage.getItem('RedActiveOpenTimes');
    //     num = (num && num != "NaN") ? Number(num) : 0;
    //     return num;
    // },

    // addRedActiveOpenTimes() {
    //     let num = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('RedActiveOpenTimes') : cc.sys.localStorage.getItem('RedActiveOpenTimes');
    //     num = (num && num != "NaN") ? Number(num) : 0;
    //     num++;
    //     cc.sys.localStorage.setItem('RedActiveOpenTimes', num);
    //     if (cc.sys.platform === cc.sys.WECHAT_GAME) {
    //         wx.setStorageSync('RedActiveOpenTimes', num)
    //     }
    // }

    getRandomRedNum() {

        // 88～98	4～6
        // 98～108	2～4
        // 108～118	1～2
        // 118～128	0.5～1
        // 128～138	0.1～0.5
        // 138～148	0.05～0.1
        // 148～150	0.01

        let moneyNum = this.getRedPacketNum();
        let randomNum;
        if (moneyNum >= 0 && moneyNum < 88) {
            randomNum = 8800;
        } else if (moneyNum >= 88 && moneyNum < 98) {
            randomNum = this.randomNum(400, 600);
        } else if (moneyNum >= 98 && moneyNum < 108) {
            randomNum = this.randomNum(200, 400);
        } else if (moneyNum >= 108 && moneyNum < 118) {
            randomNum = this.randomNum(100, 200);
        } else if (moneyNum >= 118 && moneyNum < 128) {
            randomNum = this.randomNum(50, 100);
        } else if (moneyNum >= 128 && moneyNum < 138) {
            randomNum = this.randomNum(10, 50);
        } else if (moneyNum >= 138 && moneyNum < 148) {
            randomNum = this.randomNum(5, 10);
        } else if (moneyNum >= 148 && moneyNum < 150) {
            randomNum = 1;
        } else {
            randomNum = 0;
        }
        return Number(randomNum/100);
    },

    isPopPassRed(){
        if(!this.isPopRed()) return false;
        let money = this.getRedPacketNum();
        let num = this.randomNum(1,100);
        let maxRandom = 0;
        if(money >=0 && money <=10){
            maxRandom = 100;
        }else if(money >10 && money <=20){
            maxRandom = 80;
        }else if(money >20 && money <=30){
            maxRandom = 70;
        }else if(money >30 && money <=40){
            maxRandom = 50;
        }else if(money >40 && money <=50){
            maxRandom = 30;
        }else if(money >50 && money <=60){
            maxRandom = 10;
        }else if(money >60 && money <=99){
            maxRandom = 5;
        }

        if(num <= maxRandom) return true;
        return false;
    },

    getMaxCash(){
        // let num = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('MaxCash') : cc.sys.localStorage.getItem('MaxCash');
        // if(num && num != "NaN"){
        //     num = Number(num);
        // }else{
        //     num = Math.round((94 + (Math.random() * 2)) * 100) / 100;
        // }        
        // return num;
        return 149.97;
    },

    isPopRed() {
        let money = this.getRedPacketNum();
        let maxMoney = this.getMaxCash();
        let result = (money < maxMoney) ? true : false;
        return result;
    },

    getRedActive() {
        let num = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('RedActiveOpenCpnfig') : cc.sys.localStorage.getItem('RedActiveOpenCpnfig');
        num = (num && num != "NaN") ? JSON.parse(num) : [false,false,false,false];
        return num;
    },

    setRedActive(id) {
        let num = cc.sys.platform === cc.sys.WECHAT_GAME ? wx.getStorageSync('RedActiveOpenCpnfig') : cc.sys.localStorage.getItem('RedActiveOpenCpnfig');
        num = (num && num != "NaN") ? JSON.parse(num) : [false, false, false, false];
        num[id] = true;
        let str = JSON.stringify(num);
        cc.sys.localStorage.setItem('RedActiveOpenCpnfig', str);

    },

}

